/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;
import modelo.Nave;
import dao.generic.GenericDAO;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Administrador
 */
public class NaveDAO extends GenericDAO{
    public Nave getNaveById(int idNave){
        Nave n = null;
        try{
            iniciarOperacion();
            n = (Nave) sesion.get(Nave.class, idNave);
        }finally{
            sesion.close();
        }
        return n;
    }
    public List consultaTodo(){
      List listNaves=null;
      try{
        iniciarOperacion();
        Query query = sesion.createQuery("FROM Nave");
        listNaves = query.list();
      }finally{
        sesion.close();
      }
      return listNaves;
    }
    public String[] nombreNaves(){
      List lista;
      Query query = null;
      try{
        iniciarOperacion();
        query = sesion.createQuery("select nombre from Nave");
        lista = query.list();
      }
      finally{
        sesion.close();
      }
      return (String[])lista.toArray(new String[0]);
        
    }
    public int consultaNaveByNombre(String nombreNave){
      int id=0;
      try{
        iniciarOperacion();
        Criteria criteria = sesion.createCriteria(Nave.class);
        if(!nombreNave.equals("all")){
          criteria.add(Restrictions.eq("nombre",nombreNave));
          Nave nave = (Nave) criteria.uniqueResult();
          id = nave.getId();
        }
        else{
          return -1;
        }
        
      }
      finally{
        sesion.close();
      }
      return id;
    }
}
